package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwabT;
import cn.e4j.modular.crm.entity.GwablT;
import cn.e4j.modular.crm.mapper.GwabTMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 交易单位 服务实现类
 *
 * @author yuan_rz
 * @since 2019-01-11
 */
@Service
public class Gwai200ServiceImpl extends ServiceImpl<GwabTMapper, GwabT>{

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getListData(Page<Object> page, Dto dto) {
        List<Map<String, Object>> listData = this.baseMapper.getListData(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public Boolean existedTheParams(GwabT gwabT) {
        QueryWrapper<GwabT> gwabTQueryWrapper = new QueryWrapper<>();
        gwabTQueryWrapper.eq("GWABENT",gwabT.getGWABENT());
        gwabTQueryWrapper.eq("GWAB001",gwabT.getGWAB001());
        return this.baseMapper.selectList(gwabTQueryWrapper).size() > 0;
    }

    @Transactional
    public void addData(GwabT gwabT, GwablT gwablT){
        gwabT.insert();
        gwablT.insert();
    }

    @Transactional
    public void editData(GwabT gwabT, GwablT gwablT,GwabT oldGwabT){
        //修改1
        UpdateWrapper<GwabT> gwabTUpdateWrapper = new UpdateWrapper<>();
        gwabTUpdateWrapper.eq("GWABENT",oldGwabT.getGWABENT());
        gwabTUpdateWrapper.eq("GWAB001",oldGwabT.getGWAB001());
        //修改2
        UpdateWrapper<GwablT> gwablTUpdateWrapper = new UpdateWrapper<>();
        gwablTUpdateWrapper.eq("GWABLENT",oldGwabT.getGWABENT());
        gwablTUpdateWrapper.eq("GWABL001",oldGwabT.getGWAB001());
        gwablTUpdateWrapper.eq("GWABL002",gwablT.getGWABL002());

        gwabT.update(gwabTUpdateWrapper);
        gwablT.update(gwablTUpdateWrapper);
    }

    @Transactional
    public void deleteData(GwabT gwabT, GwablT gwablT){
        delete(gwabT,gwablT);
    }

    @Transactional
    public void deleteDataBatch(List<GwabT> gwabTList,List<GwablT> gwablTList){
        for(int i=0; i < gwabTList.size(); i++){
            delete(gwabTList.get(i),gwablTList.get(i));
        }
    }

    private void delete(GwabT gwabT, GwablT gwablT){
        //修改1
        UpdateWrapper<GwabT> gwabTUpdateWrapper = new UpdateWrapper<>();
        gwabTUpdateWrapper.eq("GWABENT",gwabT.getGWABENT());
        gwabTUpdateWrapper.eq("GWAB001",gwabT.getGWAB001());
        //修改2
        UpdateWrapper<GwablT> gwablTUpdateWrapper = new UpdateWrapper<>();
        gwablTUpdateWrapper.eq("GWABLENT",gwablT.getGWABLENT());
        gwablTUpdateWrapper.eq("GWABL001",gwablT.getGWABL001());
        gwablTUpdateWrapper.eq("GWABL002",gwablT.getGWABL002());

        gwabT.delete(gwabTUpdateWrapper);
        gwablT.delete(gwablTUpdateWrapper);
    }


}
